<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${className}">
	<!-- 对象映射 -->
	<resultMap id="_${entityName}_RM" type="${className}">
		<result property="${id.name}" column="${id.column}"/>
		<#list properties as it>
		<result property="${it.name}" column="${it.column}"/>
		</#list>
	</resultMap>
	<!-- 通用条件 -->
	<sql id="_condition">
		<if test="${id.name}!=null">AND ${id.column} = ${r'#'}{${id.name}}</if>
		<#list properties as it>
		<if test="${it.name}!=null">AND ${it.column} = ${r'#'}{${it.name}}</if>
		</#list>
	</sql>
	<sql id="_orders">
		<if test="orders_ != null and !orders_.isEmpty()">
			ORDER BY 
			<foreach item="value" index="key" collection="orders_.entrySet()" separator=",">  
        		${r'$'}{key} ${r'$'}{value}
    		</foreach>
		</if>
	</sql>

	<!-- 新增数据记录 -->
	<insert id="_insert" parameterType="${className}" flushCache="true">
		<selectKey keyProperty="${id.name}" resultType="string" order="BEFORE">
			<if test="${id.name} == null  or ${id.name} == ''">SELECT UUID() FROM DUAL</if>
			<if test="${id.name} != null and ${id.name} != ''">SELECT ${r'#'}{${id.name}}</if>
		</selectKey>
		INSERT ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName}(
			${id.column}<#list properties as it>,${it.column}</#list>
		) VALUES(
			${r'#'}{${id.name}}<#list properties as it>,${r'#'}{${it.name}}</#list>
		)
	</insert>
	<!-- 按照主键ID删除记录 -->
	<delete id="_delete" parameterType="string" flushCache="true">
		DELETE FROM ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName} WHERE ${id.column} = ${r'#'}{${id.name}}
	</delete>
	<!-- 按照条件删除记录 -->
	<delete id="_delete_by_map" parameterType="${className}" flushCache="true">
		DELETE FROM ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName}
		<where>
			<include refid="_condition" />
		</where>
	</delete>

	<!-- 按照ID修改记录 -->
	<update id="_update" parameterType="${className}" flushCache="true">
		UPDATE ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName} 
		<set>
		<#list properties as it>
			${it.column} = ${r'#'}{${it.name}},
		</#list>
		</set>
		WHERE ${id.column} = ${r'#'}{${id.name}}
	</update>
	<update id="_update_by_map" flushCache="true">
		UPDATE ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName}
		<set>
		<#list properties as it>
			<if test="${it.name} != null">${it.column} = ${r'#'}{${it.name}},</if>
		</#list>
		</set>
		WHERE ${id.column} = ${r'#'}{${id.name}}
	</update>

	<!-- 按照ID取唯一记录 -->
	<select id="_select" parameterType="string" resultMap="_${entityName}_RM">
		SELECT * FROM ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName} t WHERE t.${id.column} = ${r'#'}{${id.name}}
	</select>
	<!-- 按照多个ID属性查询列表 -->
	<select id="_select_by_ins" parameterType="list" resultMap="_${entityName}_RM">
		SELECT * FROM ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName} t
		<where>
			t.${id.column} IN
			<foreach collection="list" item="item" open="(" separator="," close=")">
				${r'#'}{item}
			</foreach>
		</where>
		<include refid="_orders" />
	</select>
	<!-- 按照属性条件相等查询列表记录 -->
	<select id="_select_by_map" resultMap="_${entityName}_RM">
		SELECT * FROM ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName} t
		<where>
			<include refid="_condition" />
		</where>
		<include refid="_orders" />
	</select>

	<!-- 按照分页对象查询分页记录 -->
	<select id="_select_by_pag" parameterType="request" resultMap="_${entityName}_RM">
		SELECT * FROM ${(tableSchema?has_content)?string('${tableSchema!}.','')}${tableName} t
		<where>
		<#list properties as it>
			<if test="codex.${it.name}!=null"> AND t.${it.column} = ${r'#'}{codex.${it.name}}</if>
		</#list>
		</where>
		<include refid="_orders" />
	</select>

</mapper>